package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_wjjhl.domain.CjlkljpsyBaDetail;
import com.ruoyi.report_wjjhl.domain.vo.CjlkljpsyBaDetailVo;
import com.ruoyi.report_wjjhl.mapper.CjlkljpsyBaDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.CjlkljpsyBsInfoBo;
import com.ruoyi.report_wjjhl.domain.vo.CjlkljpsyBsInfoVo;
import com.ruoyi.report_wjjhl.domain.CjlkljpsyBsInfo;
import com.ruoyi.report_wjjhl.mapper.CjlkljpsyBsInfoMapper;
import com.ruoyi.report_wjjhl.service.ICjlkljpsyBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料颗粒级配试验检测记录水筛法Service业务层处理
 *
 * @author zyb
 * @date 2024-08-08
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class CjlkljpsyBsInfoServiceImpl implements ICjlkljpsyBsInfoService {

    private final CjlkljpsyBsInfoMapper baseMapper;
    private final CjlkljpsyBaDetailMapper detailMapper;

    /**
     * 查询粗集料颗粒级配试验检测记录水筛法
     */
    @Override
    @FIllBasicInfo
    public CjlkljpsyBsInfoVo queryById(Long excelId) {
        CjlkljpsyBsInfoVo cjlkljpsyBsInfoVo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<CjlkljpsyBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<CjlkljpsyBaDetail>()
            .eq(CjlkljpsyBaDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(baDetailVos)) {
            cjlkljpsyBsInfoVo.setBaDetailVos(baDetailVos);
        }
        return cjlkljpsyBsInfoVo;
    }

    /**
     * 查询粗集料颗粒级配试验检测记录水筛法列表
     */
    @Override
    public TableDataInfo<CjlkljpsyBsInfoVo> queryPageList(CjlkljpsyBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CjlkljpsyBsInfo> lqw = buildQueryWrapper(bo);
        Page<CjlkljpsyBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料颗粒级配试验检测记录水筛法列表
     */
    @Override
    public List<CjlkljpsyBsInfoVo> queryList(CjlkljpsyBsInfoBo bo) {
        LambdaQueryWrapper<CjlkljpsyBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CjlkljpsyBsInfo> buildQueryWrapper(CjlkljpsyBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CjlkljpsyBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, CjlkljpsyBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, CjlkljpsyBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), CjlkljpsyBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), CjlkljpsyBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), CjlkljpsyBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), CjlkljpsyBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), CjlkljpsyBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), CjlkljpsyBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), CjlkljpsyBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), CjlkljpsyBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), CjlkljpsyBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), CjlkljpsyBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), CjlkljpsyBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), CjlkljpsyBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), CjlkljpsyBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), CjlkljpsyBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), CjlkljpsyBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), CjlkljpsyBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), CjlkljpsyBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), CjlkljpsyBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), CjlkljpsyBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJilyt()), CjlkljpsyBsInfo::getJilyt, bo.getJilyt());
        lqw.eq(StringUtils.isNotBlank(bo.getHunhlmc()), CjlkljpsyBsInfo::getHunhlmc, bo.getHunhlmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGuidjpmc()), CjlkljpsyBsInfo::getGuidjpmc, bo.getGuidjpmc());
        lqw.eq(bo.getGanzsyzl1() != null, CjlkljpsyBsInfo::getGanzsyzl1, bo.getGanzsyzl1());
        lqw.eq(bo.getGanzsyzl2() != null, CjlkljpsyBsInfo::getGanzsyzl2, bo.getGanzsyzl2());
        lqw.eq(bo.getShuixhsszl1() != null, CjlkljpsyBsInfo::getShuixhsszl1, bo.getShuixhsszl1());
        lqw.eq(bo.getShuixhsszl2() != null, CjlkljpsyBsInfo::getShuixhsszl2, bo.getShuixhsszl2());
        lqw.eq(bo.getShaixl1() != null, CjlkljpsyBsInfo::getShaixl1, bo.getShaixl1());
        lqw.eq(bo.getShaixl2() != null, CjlkljpsyBsInfo::getShaixl2, bo.getShaixl2());
        lqw.eq(bo.getTonggl1() != null, CjlkljpsyBsInfo::getTonggl1, bo.getTonggl1());
        lqw.eq(bo.getTonggl2() != null, CjlkljpsyBsInfo::getTonggl2, bo.getTonggl2());
        lqw.eq(bo.getPingj() != null, CjlkljpsyBsInfo::getPingj, bo.getPingj());
        lqw.eq(bo.getGanshzl1() != null, CjlkljpsyBsInfo::getGanshzl1, bo.getGanshzl1());
        lqw.eq(bo.getGanshzl2() != null, CjlkljpsyBsInfo::getGanshzl2, bo.getGanshzl2());
        lqw.eq(bo.getSunh1() != null, CjlkljpsyBsInfo::getSunh1, bo.getSunh1());
        lqw.eq(bo.getSunh2() != null, CjlkljpsyBsInfo::getSunh2, bo.getSunh2());
        lqw.eq(bo.getSunhl1() != null, CjlkljpsyBsInfo::getSunhl1, bo.getSunhl1());
        lqw.eq(bo.getSunhl2() != null, CjlkljpsyBsInfo::getSunhl2, bo.getSunhl2());
        lqw.eq(bo.getKoucshhzl1() != null, CjlkljpsyBsInfo::getKoucshhzl1, bo.getKoucshhzl1());
        lqw.eq(bo.getKoucshhzl2() != null, CjlkljpsyBsInfo::getKoucshhzl2, bo.getKoucshhzl2());
        return lqw;
    }

    /**
     * 新增粗集料颗粒级配试验检测记录水筛法
     */
    @Override
    public Boolean insertByBo(CjlkljpsyBsInfoBo bo) {
        CjlkljpsyBsInfo add = BeanUtil.toBean(bo, CjlkljpsyBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 14次
            long size = 14;
            ArrayList<CjlkljpsyBaDetail> details = new ArrayList<>((int) size);
            for (long i = 1; i <= size; i++) {
                details.add(new CjlkljpsyBaDetail(add.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改粗集料颗粒级配试验检测记录水筛法
     */
    @Override
    public Boolean updateByBo(CjlkljpsyBsInfoBo bo) {
        CjlkljpsyBsInfo update = BeanUtil.toBean(bo, CjlkljpsyBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos(), CjlkljpsyBaDetail.class));
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CjlkljpsyBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料颗粒级配试验检测记录水筛法
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
